<?php
namespace app\channel\controller;
use think\Db;

class AuditList extends Base
{
    //只有CPS一级渠道可以访问
    public function _initialize(){
        parent::_initialize();
        if(!session('admin.cps_id') || session('admin.cps_pid')!=0){
            $this->error('访问错误');
        }
    }

    //CPS对账列表
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');

            $where['cps_id'] = session('admin.cps_id');
            if( ! $count=Db::name('CpsBill')->where($where)->count() ){
                $this->layTable();
            }

            $select = Db::name('CpsBill')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('create_time desc')
                ->select();

            $data = [];
            $channelIdName = model('manage/Channel')->channelIdName();
            $cpsAuditStatus = config('cps_audit_status');
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'daytime' => date('Y/m/d', $v['start_time']) . ' - ' . date('Y/m/d', $v['end_time']),
                    'cps_id' => $v['cps_id'],
                    'cps_name' => $channelIdName[ $v['cps_id'] ],
                    'pay_rate' => $v['pay_rate'] . '%',
                    'channel_rate' => $v['channel_rate'] . '%',
                    'tax_rate' => $v['tax_rate'] . '%',
                    'fact_money' => $v['fact_money'],
                    'end_money' => $v['end_money'],
                    'create_time' => date('Y-m-d H:i:s', $v['create_time']),
                    'status' => $cpsAuditStatus[ $v['status'] ],
                    'contact_man' => $v['contact_man'],
                    'open_account' => $v['open_account'],
                    'open_name' => $v['open_name'],
                    'open_bank' => $v['open_bank'],
                    'description' => htmlspecialchars_decode($v['description']),
                ];
            }

            $this->layTable(0, 'CPS对账列表', $data, $count);
        }else{
            $cpsWeek = Db::name('ViewChannel')->where('cps_id', session('admin.cps_id'))->value('cps_week');
            //如果为固定期结算的渠道
            if($cpsWeek){
                $disabledBtn = 0;
            }else{
                $find = Db::name('CommonOption')->field('name,value')->where('id', 222)->find();

                //如果后台设置了限制申请时间
                if(!empty($find['name']) && !empty($find['value'])){
                    $startDate = $find['name'];
                    $endDate = $find['value'];

                    //如果当前时间在限制的申请时间内，则不显示申请
                    if((time() >= $startDate) && (time() <= $endDate)){
                        $disabledBtn = 0;
                    }else{
                        $disabledBtn = 1;
                    }
                    //后台没有限制申请时间
                }else{
                    //如果当前时间为周一，则不显示申请
                    if(date("w") == '1'){
                        $disabledBtn = 0;
                    }else{
                        $disabledBtn = 1;
                    }
                }
            }

            $notice1 = Db::name('CommonOption')->where('id', 250)->value('big_des');
            $notice2 = Db::name('CommonOption')->where('id', 260)->value('big_des');

            return view('', ['notice1'=>$notice1, 'notice2'=>htmlspecialchars_decode($notice2), 'disabled_btn'=>$disabledBtn]);
        }
    }

    public function view(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');

            $where['bill_id'] = input('id');
            if( ! $count=Db::name('CpsBillDetail')->where($where)->count() ){
                $this->layTable();
            }

            $select = Db::name('CpsBillDetail')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('money desc')
                ->select();

            $data = [];
            $channelIdName = model('manage/Channel')->channelIdName();
            $gameIdName = model('manage/Game')->gameIdName();
            $find = Db::name('CpsBill')->where('id', input('id'))->find();
            foreach ($select as $k=>$v){
                $data[] = [
                    'cps_id' => $v['cps_id'],
                    'cps_name' => $channelIdName[ $v['cps_id'] ],
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'money' => $v['money'],
                    'pay_rate' => $find['pay_rate'] . '%',
                    'channel_rate' => $find['channel_rate'] . '%',
                    'tax_rate' => $find['tax_rate'] . '%',
                    'end_money' => round($v['money']*($find['pay_rate']/100)*(1-$find['channel_rate']/100)*(1-$find['tax_rate']/100), 2),
                ];
            }

            $this->layTable(0, 'CPS对账 > 查看', $data, $count);
        }else{
            return view();
        }
    }

    public function sure(){
        $update = [
            'sure_time'=>time(),
            'status' => 2
        ];
        $updateRs = Db::name('CpsBill')->where('id', input('id'))->update($update);
        $updateRs!==false ? $this->layJson(0, '确认成功') : $this->layJson(1, '确认失败，请联系米娱客服人员');
    }

    public function apply(){
        $monday = strtotime(date('Y/m/d',(time()-((date('w')==0?7:date('w'))-1)*24*3600)));
        $sunday = strtotime(date('Y/m/d',(time()+(7-(date('w')==0?7:date('w')))*24*3600)))+86399;

        $where['cps_id'] = session('admin.cps_id');
        $where['apply_time'] = ['between', [$monday,$sunday]];
        if( Db::name('CpsBillApply')->where($where)->count() ){
            $this->layJson(1, "申请失败，本周您已申请过结算！");
        }

        $insert['cps_id'] = session('admin.cps_id');
        $insert['apply_time'] = time();
        $insert['status'] = 0;
        if( Db::name('CpsBillApply')->insert($insert) ){
            $this->layJson(0, '申请成功，后台将会在指定时间给您发送账单');
        }else{
            $this->layJson(1, '申请失败，请联系官方系统');
        }
    }
}